package com.hissagetest;

import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;

public class HelloUiTest extends Activity {
	private interface MenuId {
		public static final int Item1 	   = 1000 ;
		public static final int SubMenBase = 2000 ;
		public static final int ContextMenu1Base = 3000 ;
		public static final int ContextMenu2Base = 4000 ;
	} ;
	
	private Button layoutTestButton ;
	private HelloUiTest self ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE) ;
		setContentView(R.layout.ui_test) ;
		
		registerForContextMenu(findViewById(R.id.MenuButton1)) ;
		registerForContextMenu(findViewById(R.id.MenuButton2)) ;
		
		self = this ;
		layoutTestButton = (Button)findViewById(R.id.layoutTestButton) ;
		layoutTestButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(self, HelloLayout.class) ;
				startActivity(intent) ;
			}
		}) ;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) 
			Log.i("UiTest", "onKeyDown get the back key event") ;
			
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.option_menu, menu);
	    menu.add(0, MenuId.Item1, MenuId.Item1, "addByCode") ;
	    menu.add("addByCodeNoId").setIcon(R.drawable.no_id) ;
	    SubMenu subMenu = menu.addSubMenu("SubMenu") ;
	    
	    subMenu.add(0, MenuId.SubMenBase + 1, MenuId.SubMenBase + 1, "SubMen1") ;
	    subMenu.add(0, MenuId.SubMenBase + 2, MenuId.SubMenBase + 2, "SubMen2") ;
	    subMenu.add(0, MenuId.SubMenBase + 3, MenuId.SubMenBase + 3, "SubMen3") ;
	    subMenu.setIcon(R.drawable.sub_menu);
	    subMenu.setHeaderIcon(R.drawable.sub_menu_head);
	    
	    menu.add("more0") ;
	    menu.add("more1").setIcon(R.drawable.more1); /* NO effect */
	    menu.add("more2") ;
	    menu.add("more3") ;
	    return true;

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		String showStr = null ;
		
		switch (item.getItemId()) 
		{
		case R.id.OptionItem1:
			showStr = "Item1 selected" ;
			break ;
			
		case R.id.OptionItem2:
			showStr = "Item2 selected" ;
			break ;
			
		case MenuId.Item1:
			showStr = "Custom Item1 selected" ;
			break ;
			
		default:
			showStr = String.format("unhandle item = %d selected", item.getItemId()) ;
			return true;
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this) ;
		builder.setMessage(showStr)
		.setCancelable(true)
		.setNegativeButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel() ;
			}
		}) ;
		
		AlertDialog dlg = builder.create() ;
		dlg.setOwnerActivity(this) ;
		dlg.show();
		
		return true ;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		String preStr = null ;
		int base = 0 ;
		if (v == findViewById(R.id.MenuButton1)) {
			preStr = "ContextMenu1_" ;
			menu.setHeaderTitle("ContextMenu1") ;
			menu.setHeaderIcon(R.drawable.context_menu1) ;
			base = MenuId.ContextMenu1Base ;
		} else {
			preStr = "ContextMenu2_" ;
			base = MenuId.ContextMenu2Base ;
		}
		
		menu.add(0, base + 1, base + 1, preStr + 1) ;
		menu.add(0, base + 2, base + 2, preStr + 2) ;
		menu.add(0, base + 3, base + 3, preStr + 3) ;
		menu.add(0, base + 4, base + 4, preStr + 4) ;
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		String showStr = null ;
		int itemId = item.getItemId() ;
		
		if (itemId < MenuId.ContextMenu2Base)
			showStr = String.format("%s item = %d selected", "ContextMenu1", itemId) ;
		else
			showStr = String.format("%s item = %d selected", "ContextMenu2", itemId) ;
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this) ;
		builder.setMessage(showStr)
		.setCancelable(true)
		.setNegativeButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel() ;
			}
		}) ;
		
		AlertDialog dlg = builder.create() ;
		dlg.setOwnerActivity(this) ;
		dlg.show();
		
		return true ;
	}
} 